package com.whh.db;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;
import org.apache.commons.io.FileUtils;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.File;
import java.io.StringReader;
import java.util.List;

public class PjwsModel extends Model<PjwsModel> {
    public static PjwsModel dao = new PjwsModel();

    public static void main(String[] args) throws Exception {
        String filePath = "F:\\word2vec\\347.txt";
        int xfId = 347;
        db2file(filePath, xfId);
    }

    public static void db2file(String filePath, int xfId) throws Exception{
        InitMySql.getInstance().start();
        int count = 50;
        int end = 10000;
        int start = 0;
        File file = new File(filePath);
        for (int i = 1; i < count; i++) {
            if (i != 1) {
                start = (i - 1) * end + 1;
            }
            System.out.println(start + " -- " + i * end);
            List<PjwsModel> list = PjwsModel.dao.find("select * from pjws_bak where xf_id = " + xfId + " and id between " + start + " and " + i * end);
            String content = "";
            for (PjwsModel model : list) {
                content = model.getStr("content").replaceAll("<[/]?[^<>]+>", "")+"\r\n";
                content = segmentWord(content);
                System.out.println(content);
                FileUtils.writeStringToFile(file, content, "UTF-8", true);
            }
        }
    }

    public static String segmentWord(String line) throws Exception {
        IKSegmenter ikSeg = new IKSegmenter(new StringReader(line), true);
        Lexeme lexeme = null;
        StringBuffer sb = new StringBuffer();
        while ((lexeme = ikSeg.next()) != null) {
            sb.append(lexeme.getLexemeText()).append(" ");
        }
       return sb.toString();
    }
}
